import { InspectionItemAddComponent } from './inspection-item-add/inspection-item-add.component';
import { JobOrderProcess } from './../../../@core/data/job';

import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { DataTableComponent, DialogService, TableWidthConfig } from '@devui';
import { EncodeRulesService } from 'src/app/@core/services/encoderRule/encode-rules.service';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { InspectionItemEditComponent } from './inspection-item-edit/inspection-item-edit.component';
import { AttachmentComponent } from 'src/app/@shared/components/attachment/attachment.component';

@Component({
  selector: 'app-inspection-item',
  templateUrl: './inspection-item.component.html',
  styleUrls: ['./inspection-item.component.scss']
})
export class InspectionItemComponent {
   tabledata:any;//编码规则
    pager = {
      total: 20,
      pageIndex: 1,
      pageSize: 20
    };
    checkedIds = [];
    pageSizeOptions = [10, 20, 30, 50, 100];
    msgs: { severity: any; summary: any; content: any; }[];
    config = {
      id: 'Add-Dirctoy-Type',
      maxHeight: '400px',
      width: '400px',
      backdropCloseable: true,
    }//弹窗配置
    SearchTerm:string='';
    @ViewChild(DataTableComponent, { static: true }) datatable: DataTableComponent;
    checkboxList = [];
    allChecked = false;
    halfChecked = false;
    file: any;
    constructor(private EncodeRulesService:EncodeRulesService,private Router:Router,private dialogService: DialogService,private SharedMethodService:SharedMethodService,private JobOrderService:JobOrderService){}
    ngOnInit(): void {
      this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
        this.pager.total=data.totalCount;
      })//获取工序
    }
    dataTableOptions = {
      columns: [
        {
          field: 'showType',
          header: '检验项名称',
          fieldType: 'text'
        },
        {
          field: 'type',
          header: '检验描述',
          fieldType: 'text'
        },
        {
          field: 'enabled',
          header: '检验单位',
          fieldType: 'boolean'
        },
        {
          field: 'enabled',
          header: '附件',
          fieldType: 'boolean'
        }
      ]
    };//表格数据
    tableWidthConfig: TableWidthConfig[] = [
      {
        field: 'checkbox',
        width: '80px'
      },
      {
        field:'caozuo',
        width:'150px'
      },
      {
        field: 'showType',
        width: '250px'
      },
      {
        field: 'type',
        width: '250px'
      },
      {
        field: 'type',
        width: '250px'
      },
      {
        field: 'enabled',
        width: '150px'
      }
    ];//表格宽度配置
    pageIndexChange($event: any) {
      this.pager.pageIndex=$event;
      this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
      })//获取工序
    }
    pageSizeChange($event: any){
      this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
      })//获取工序
    }//
    Delete(id:any,dialogtype?:string){
      const results = this.dialogService.open({
         maxHeight: '400px',
      width: '400px',
      backdropCloseable: true,
        title: '删除',
        content: '确认删除该检验项？',
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
                this.JobOrderService.DeleteInspectionProject(id).subscribe((data:any)=>{
                  results.modalInstance.hide();
                  this.showToast('success','删除成功','该检验项删除成功')
                  this.checkedIds=[];
                  this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                    this.tabledata=data.items;
                    this.pager.total=data.totalCount;
                  })//获取工序
                },(error) => {
                  if(error.status==404){
                    this.showToast('error', '失败', '服务器连接失败');
                  }else{
                    this.showToast('error', '失败', error.error);
                  }
                }
                )
            }
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    }//删除
    edit(rowItem:any,dialogtype?:string){
      const data  = {
        display:false,
        data:rowItem
      }
      const results = this.dialogService.open({
        maxHeight: '400px',
        width: '400px',
        backdropCloseable: true,
        title: '编辑检验项',
        data:data,
        content: InspectionItemEditComponent,
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              if(results.modalContentInstance.inspection.inspectionProjectName=='' || results.modalContentInstance.inspection.inspectionProjectName==null){
                this.showToast('error','失败','检验项名称不能为空')
                data.display = true
                return
              }
              this.JobOrderService.UpdateInspectionProject(rowItem.inspectionProjectId,results.modalContentInstance.inspection).subscribe((data:any)=>{
                this.showToast('success','成功','检验项编辑成功')
                results.modalInstance.hide();
                this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tabledata=data.items;
                  this.pager.total=data.totalCount;
                })
              },(error)=>{
                this.showToast('error','失败',error)
                console.log(error)
              }
            )
            }
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    }//编辑
    filter(){
      this.pager.pageIndex=1;
      this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
      })//获取工序
    }//筛选
    reset(){
      this.SearchTerm='';
      this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
        this.tabledata=data.items;
      })//获取工序
    }//重置
    showToast(type: any,summary:any,content:any) {
      switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
      }
    }//提示框
    cleared(){
      this.SharedMethodService.cleared(this.checkedIds,this.tabledata,this.datatable).subscribe((data:any)=>{
        this.checkedIds=data;
      })
    }
    onRowCheckChange(checked: any, rowIndex: any, nestedIndex: any, rowItem: { $checked: any; $halfChecked: boolean; }) {
      this.SharedMethodService.onRowCheckChange(checked, rowIndex, nestedIndex, rowItem,this.datatable);
    }//单选
    getCheckedRows() {
      this.SharedMethodService.getCheckedRows(this.datatable,'processId').subscribe((data:any)=>{
        this.checkedIds=data;

      })
    }//获取选中行
    add(dialogtype?:string){
      const data  = {
        display:false
      }
      const results = this.dialogService.open({
        maxHeight: '400px',
        width: '400px',
        backdropCloseable: true,
        title: '新增检验项',
        data:data,
        content: InspectionItemAddComponent,
        dialogtype: dialogtype,
        onClose: () => {
        },
        buttons: [
          {
            cssClass: 'stress',
            text: '确定',
            handler: ($event: Event) => {
              if(results.modalContentInstance.inspection.inspectionProjectName=='' || results.modalContentInstance.inspection.inspectionProjectName==null){
                this.showToast('error','失败','检验项名称不能为空')
                data.display = true
                return
              }
              this.JobOrderService.CreateInspectionProject(results.modalContentInstance.inspection).subscribe((data:any)=>{
                this.showToast('success','成功','新增检验项成功')
                results.modalInstance.hide();
                this.JobOrderService.GetPagedInspectionProject(this.SearchTerm,this.pager.pageIndex,this.pager.pageSize).subscribe((data:any)=>{
                  this.tabledata=data.items;
                  this.pager.total=data.totalCount;
                } )
              },(error)=>{
                this.showToast('error','失败',error)
                console.log(error)
              }
            )
            }
          },
          {
            id: 'btn-cancel',
            cssClass: 'common',
            text: '取消',
            handler: ($event: Event) => {
              results.modalInstance.hide();
            },
          },
        ],
      });
    }
    fujian(rowitem: any, dialogtype?: string) {
        const results = this.dialogService.open({
          title: '附件',
          width: '800px',
          backdropCloseable: false,
          data: {
            data: '销售附件',
            urls: rowitem.attachments,
            type: '查看'
          },
          content: AttachmentComponent,
          dialogtype: dialogtype,
          onClose: () => {
          },
          buttons: [
            {
              cssClass: 'stress',
              text: '确定',
              handler: ($event: Event) => {
                results.modalInstance.hide();
              },
            },
            {
              id: 'btn-cancel',
              cssClass: 'common',
              text: '取消',
              handler: ($event: Event) => {
                results.modalInstance.hide();
              },
            },
          ]
        });
      }
}
